package com.example.pet.dao;

import com.hj.pet.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoDao {
    @Insert("insert into user_info(user_id,user_account,user_name,phone,address,is_seller,is_vip,favorites,url,details,birth,create_time,modify_time)" +
            "values(#{user_id},#{user_account},#{user_name},#{phone},#{address},#{is_seller},#{is_vip},#{favorites},#{url},#{details},#{birth},now(),now())")
    int save(UserInfo userInfo);

    @Update("<script>" +
            "update user_info" +
            "        <set>" +
            "                <if test=\"null !=user_name\">" +
            "                    user_name=#{user_name}," +
            "                </if>" +
            "                <if test=\"null !=phone\">" +
            "                    phone=#{phone}," +
            "                </if>" +
            "                <if test=\"null !=address\">" +
            "                    address=#{address}," +
            "                </if>" +
            "                <if test=\"null !=favorites\">" +
            "                    favorites=#{favorites}," +
            "                </if>" +
            "                <if test=\"null !=url\">" +
            "                    url=#{url}," +
            "                </if>" +
            "                <if test=\"details\">" +
            "                    details=#{details}," +
            "                </if>" +
            "        </set>" +
            "        where user_id=#{user_id}"
            +"</script>")
    int updateById(UserInfo userInfo);

    @Select("select * from user_info where user_id=#{user_id}")
    UserInfo findUserById(int userId);

    @Select("select * from user_info where user_account=#{user_account}")
    UserInfo findUserByAc(String user_account);

    @Select("select * from user_info where is_seller=#{is_seller}")
    List<UserInfo> findUsersSeller(boolean is_seller);

    @Select("select * from user_info where is_vip=#{is_vip}")
    List<UserInfo> findUsersVip(boolean is_vip);

    @Delete("delete from user_info where user_id=#{user_id}")
    int delUserById(int userId);

    //TODO 多条件查询
    @Select("<script>" +
            "select * from user_info" +
            "        <where>" +
            "            <if test=\"null !=user_name\">" +
            "                and user_name like concat('%',#{user_name},'%')" +
            "            </if>" +
            "            <if test=\"null !=is_seller\">" +
            "                and is_seller=#{is_seller}" +
            "            </if>" +
            "            <if test=\"null !=is_vip\">" +
            "                and is_vip=#{is_vip}" +
            "            </if>" +
            "        </where>"
            +"</script>")
    List<UserInfo> findUsers(UserInfo userInfo);

    @Select("select count(*) from user where user_account={account}")
    int searchByUserAccount(String account);
}
